package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@Api(tags = "菜品相关接口")
@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    @Transactional
    public Result insert(@RequestBody DishDTO dishDTO)
    {log.info(String.valueOf(dishDTO));
     dishService.save(dishDTO);
     return Result.success();
    }
    @ApiOperation("分页查询")
        @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO)
    {
     PageResult pageResult=dishService.page(dishPageQueryDTO);

     return Result.success(pageResult);
    }

    /**
     *
     * @param id
     * @return
     *
     */
    @ApiOperation("根据ID获取值")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id)
    {
        DishVO dish =dishService.getById(id);
        return Result.success(dish);
    }
    @ApiOperation("根据Id批量删除")
    @DeleteMapping
    public Result delete(@RequestParam("ids")List<Long> ids)
    {
        dishService.delete(ids);
        return Result.success();
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO)
    {
        dishService.update(dishDTO);
    return Result.success();
    }

    @ApiOperation("根据分类查询")
    @GetMapping("/list")
    public Result getTypeId(@RequestParam("categoryId") Integer id)
    {   List<Dish> dishes=dishService.getTypeId(id);
    return Result.success(dishes);
    }

    @ApiOperation("启用禁用")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id)
    {   dishService.startOrStop(status,id);
    return Result.success();
    }
}
